<?php

namespace app\home\controller;

use app\home\model\pyg_category;
use app\home\model\pyg_goods;
use app\home\model\pyg_order_goods;
use app\home\model\pyg_spec;
use app\home\model\pyg_spec_goods;
use app\home\model\pyg_spec_value;
use think\facade\Db;
use think\facade\View;

class Details
{
    public function item($id)
    {
        $obe = new pyg_category();
        $res = $obe->showCategory()->toArray();
        $showCategory = get_tree_list($res);
        $obj = new pyg_goods();
        $data = $obj->with(['pygGoods','pygImage'])->withoutField('delete_time,create_time,update_time')->find($id)->toArray();
        $data['goods_attr'] = json_decode($data['goods_attr'],true);
        $valueIds = array_unique(explode("_",implode("_",array_column($data['pygGoods'],'value_ids'))));
        $obg = new pyg_spec_value();
        $values = $obg->with('showSpec')->whereIn('id',$valueIds)->select()->toArray();
        $res = [];
        foreach ($values as $val)
        {
            $res[$val['spec_id']]=[
                'spec_id'=>$val['spec_id'],
                'spec_name'=>$val['spec_name'],
                'spec_values'=>[]
            ];
        }
        foreach ($values as $val)
        {
            $res[$val['spec_id']]['spec_values'][]=$val;
        }
        $obt = new pyg_goods();
        $package = $obt->with('package')->find($id)->toArray();
        return View::fetch('goods/item',['data'=>$data,'showCategory'=>$showCategory,'res'=>$res,'package'=>$package]);
    }
    public function moy($id)
    {
        $res = Db::table('pyg_spec_goods')->find($id);
        if($res)
        {
            return success('查询成功',$res);
        }
        return fail('查询失败',$res);
    }
}